unit cominginvoiceslist;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  DBGrids;

type

  { TfrmComingInvoicesList }

  TfrmComingInvoicesList = class(TForm)
    btnAdd: TBitBtn;
    btnEdit: TBitBtn;
    btnDelete: TBitBtn;
    btnClose: TBitBtn;
    DBGComingInvoicesList: TDBGrid;
    procedure btnAddClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure DBGComingInvoicesListDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure checkErrors;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmComingInvoicesList: TfrmComingInvoicesList;

implementation

{$R *.lfm}
uses main,cominginvoiceelement;

{ TfrmComingInvoicesList }

procedure TfrmComingInvoicesList.checkErrors;
begin
  if (DBGComingInvoicesList.DataSource.DataSet.RecordCount=0) then begin
    btnEdit.Enabled:=False;
    btnDelete.Enabled:=False;
  end else begin
    btnEdit.Enabled:=True;
    btnDelete.Enabled:=True;
  end;
end;

procedure TfrmComingInvoicesList.btnAddClick(Sender: TObject);
begin
  frmComingInvoiceElement.invoiceIsNew:=True;
  frmComingInvoiceElement.ShowModal;
  checkErrors;
end;

procedure TfrmComingInvoicesList.btnDeleteClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Для правильного видалення потрібно зайти в накладну і видалити товари по одному після чого видалити накладну, продовжити незважаючи на це?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    frmMain.execQuery('DELETE FROM comingsgoods WHERE invoiceid='+DBGComingInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.execQuery('DELETE FROM comingsofinvoicesongoods WHERE id='+DBGComingInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.history('Прихідна накладна','Видалення накладної № '+DBGComingInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    FrmMain.DSComingInvoices.DataSet.Refresh;
    checkErrors;
  end;
end;

procedure TfrmComingInvoicesList.btnEditClick(Sender: TObject);
begin
  frmComingInvoiceElement.lblEdtCounterParty.Text:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('counterpartytitle').AsString;
  frmComingInvoiceElement.lblEdtStoreHouse.Text:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('storehousetitle').AsString;
  frmComingInvoiceElement.lblEdtInvoiceID.Text:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('invoiceid').AsString;
  frmComingInvoiceElement.ginvoiceid:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('id').AsInteger;
  FrmMain.storeHouseSelectedId:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('storehouseid').AsInteger;
  FrmMain.counterPartieSelectedId:=DBGComingInvoicesList.DataSource.DataSet.FieldByName('counterpartyid').AsInteger;
  frmComingInvoiceElement.invoiceIsNew:=False;
  frmComingInvoiceElement.ShowModal;
  checkErrors;
end;

procedure TfrmComingInvoicesList.DBGComingInvoicesListDblClick(Sender: TObject);
begin
  if (DBGComingInvoicesList.DataSource.DataSet.RecordCount>0) then begin
    btnEdit.Click;
  end;
end;

procedure TfrmComingInvoicesList.FormShow(Sender: TObject);
begin
  DBGComingInvoicesList.DataSource.DataSet.Last;
  checkErrors;
end;

end.

